﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;

using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


using WpfDataBingding.DevTreeData;
using bus=WpfDataBingding.TaskServiceReference;

namespace WpfDataBingding
{
    /// <summary>
    /// Page1.xaml 的交互逻辑
    /// </summary>
    public partial class MonTaskConfigMainPage : Page
    {

        public MonTaskConfigMainPage()
        {
            InitializeComponent(); 
            try
            {
                //WpfDataBingding.DevTreeData.DataBus dataBus = (Application.Current.Properties["dataBus"] as WpfDataBingding.DevTreeData.DataBus);
                //this.DevTreeView.DataContext = dataBus.MonitorAgentList;

                refresh_DevTree();                
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e.Message);
            }                      
        }

        /// <summary>
        /// 调用远程服务获取设备集合并绑定到设备树控件
        /// </summary>
        private void refresh_DevTree()
        {
            bus.DataBusServiceClient dBSC = new bus.DataBusServiceClient();
            bus.MonAgentInfo[] monAgentInfoArray = null;            
            try
            {
                //TaskServiceReference.DeviceType[] dt= dBSC.QueryAllDeviceTypeInfo()  ;
                
                //调用远程服务
                monAgentInfoArray = dBSC.QueryMonAgentsDevs();
                
                DataBus db = new DataBus();
                db.initFromDataContractObj(monAgentInfoArray);
                this.DevTreeView.DataContext = db.MonitorAgentList;
            }
            catch (Exception exp)
            {
                MessageBox.Show("远程获取设备集合出错："+exp.Message);
                return;
            }
            finally
            {
                if (dBSC!=null)
                    dBSC.Close();
            }
        }

        /// <summary>
        /// 刷新设备树
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            refresh_DevTree();
        }

        /// <summary>
        /// DevTreeView树双击事件处理函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DevTreeView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            try
            {
                string userID = (string)Application.Current.Properties["userID"];//////////////debug

                Type selectedItemType = this.DevTreeView.SelectedItem.GetType();
                Type DeviceType = Type.GetType("WpfDataBingding.DevTreeData.Device");
                Type MonitorAgentType = Type.GetType("WpfDataBingding.DevTreeData.MonitorAgent");

                if (selectedItemType == DeviceType)//设备信息详细界面
                {
                    Device dev_Selected = (Device)(this.DevTreeView.SelectedItem);
                    //判断设备是否被监控 
                    //刷新              
                    dev_Selected.addDeviceMoniterInfo(userID);

                    DevDetail devDetail = new DevDetail();//建立显示对象
                    devDetail.DataContext = dev_Selected;//设置数据源
                    this.detail_groupBox.Content = devDetail;//显示在detail_groupBox
                }

                else if (selectedItemType == MonitorAgentType)//监控代理信息详细界面
                {
                    //获取数据源
                    MonitorAgent monAgent_Selected = (MonitorAgent)(this.DevTreeView.SelectedItem);
                    //判断该监控代理设备是否被监控，对应每个设备监控状态进行初始化
                    foreach (Device dev in monAgent_Selected.DeviceList)
                    {
                        dev.addDeviceMoniterInfo(userID);
                    }
                    MonAgentDetail monAgentDetail = new MonAgentDetail(monAgent_Selected);//建立显示对象并设置数据源                    
                    //显示在detail_groupBox
                    this.detail_groupBox.Content = monAgentDetail;
                }
            }
            catch (Exception exp)
            {
                MessageBox.Show("出错：" + exp.Message);
            }

        }

        private void button1_Click_1(object sender, RoutedEventArgs e)
        {
            this.NavigationService.Navigate(new WpfDataBingding.DeviceMonitor());
        }

        /// <summary>
        /// debug drag_drop
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Grid_Drop(object sender, DragEventArgs e)
        {
            MessageBox.Show("123");
        }



    }



}
